package com.weetop.akly.activity.meessage;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.bean.SignDayBean;
import com.weetop.akly.presenter.SignDetailPresent;
import com.weetop.akly.utils.TimeUtils;
import com.weetop.akly.view.SignDetailView;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignDayDetailActivity extends BaseActivity<SignDetailPresent> implements CalendarView.OnCalendarSelectListener, SignDetailView {

    @BindView(R.id.calendarView)
    CalendarView mCalendarView;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.rl_data)
    LinearLayout rlData;
    @BindView(R.id.calendarLayout)
    CalendarLayout calendarLayout;
    private int curYear;
    private int curMonth;
    private String mCurDay;


    @Override
    protected SignDetailPresent createPresenter() {
        return new SignDetailPresent(this, this);
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        int year = calendar.getYear();
        int month = calendar.getMonth();
        int day = calendar.getDay();
        String curMonth = year + "-" + (month < 10 ? ("0" + month) : month);
        String curDay = year + "-" + (month < 10 ? ("0" + month) : month) + "-" + (day < 10 ? ("0" + day) : day);
        tvDate.setText(year + "年" + month + "月");
        if (isClick) {
            mPresenter.getDayCheckinWatch(curDay);
        } else {
            mPresenter.getCheckinWatch(curMonth);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_day_detail);
        ButterKnife.bind(this);
        Long signDay = getIntent().getLongExtra("sign_day", 0);
        mCalendarView.setOnCalendarSelectListener(this);
        String curMonth = TimeUtils.fromLongToDate("yyyy-MM", signDay * 1000);
        mCurDay = TimeUtils.fromLongToDate("yyyy-MM-dd", signDay * 1000);

        tvDate.setText(TimeUtils.fromLongToDate("yyyy", signDay * 1000) + "年" + TimeUtils.fromLongToDate("MM", signDay * 1000) + "月");
        mCalendarView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mCalendarView.scrollToCalendar(Integer.valueOf(TimeUtils.fromLongToDate("yyyy", signDay * 1000)), Integer.valueOf(TimeUtils.fromLongToDate("MM", signDay * 1000)), Integer.valueOf(TimeUtils.fromLongToDate("dd", signDay * 1000)));
            }
        }, 500);

        mPresenter.getCheckinWatch(curMonth);
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    @Override
    public void getCheckinWatchSuccess(BaseModel<List<SignDayBean>> o) {
        List<SignDayBean> data = o.getData();
        initData(data);
        if (!TextUtils.isEmpty(mCurDay)) {
            mPresenter.getDayCheckinWatch(mCurDay);
            mCurDay = null;
        }
    }

    private void initData(List<SignDayBean> data) {
        Map<String, Calendar> map = new HashMap<>();
        System.out.println("打卡数据：" + data.toString());
        if (data == null || data.size() <= 0) {
            return;
        }
        for (SignDayBean key : data) {
            String[] dateArr = key.getDates().split("-");
            //0：灰色-缺省   1：蓝色-正常     2：黄色-迟到或早退  3：红色-旷工
            String state = key.getStatus() + "";
            int color;
            if (state.equals("1")) {
                state = "正常";
                color = 0xFF32CD32;
            } else if (state.equals("2")) {
                state = "外勤";
//                0xFFFFA500  黄色
                color = 0xFF00BFFF;
            } else if (state.equals("3")) {
                state = "迟到";
                color = 0xFFFF4500;
            } else if (state.equals("4")) {
                state = "早退";
                color = 0xFFFFA500;
            } else if (state.equals("5")) {
                state = "旷工";
                color = 0xFF9370DB;
            } else {
                state = "缺省";
                color = 0xFF808080;
            }
            Calendar schemeCalendar = getSchemeCalendar(Integer.valueOf(dateArr[0]), Integer.valueOf(dateArr[1]), Integer.valueOf(dateArr[2]), color, state);
            map.put(schemeCalendar.toString(), schemeCalendar);
        }
        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        mCalendarView.setSchemeDate(map);
    }


    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }

}
